﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using NLog;
using dlCentral.Utils;
using dlCentral.PluginHandlers;
using Cornerstone.Tools;
using System.Linq;
using System.Text;
using dlCentral.Settings.Data;


namespace dlCentral.TorrentHandlers {
    internal abstract class TorrentHandler {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        public string url = "";
        public string SearchTerm = "";
        public string Year = "";
        public string Category = "";
        public SiteSearchType DLSType = SiteSearchType.NONE;
        public SettingsSite Site = null; 
        public TorrentHandlerType Type
        {
            get
            {
                return GetTorrentHandlerType();
            }
        }

        // The torrent ID for the plugin this handler implements support for.
        public abstract TorrentId ID
        {
            get;
            set;
        }

        // Should return the name of the source Torrent providing the data.
        public abstract string TorrentName {
            get;
            set;
        }

        public abstract void GetDetails(List<MediaDetail> Results);
        


        // Should return true if all required components for this plugin are present.
        // See IsActive() abstract method below.
        public bool Available {
            get {
                try {
                    return IsAvailable();
                }
                catch (Exception) { }
                return false;
            }
        }


        // Should return true if all required components for this plugin are present.
        protected abstract bool IsAvailable();

        protected virtual TorrentHandlerType GetTorrentHandlerType() {
            return TorrentHandlerType.BASIC;
        }

        protected static string get(string url)
        {
            WebGrabber grabber = dlCentralUtils.GetWebGrabberInstance(url);
            grabber.Encoding = Encoding.UTF8;

            if (grabber.GetResponse())
                return grabber.GetString();
            else
                return null;
        }
        protected static System.Xml.XmlNodeList getXML(string url)
        {
            WebGrabber grabber = dlCentralUtils.GetWebGrabberInstance(url);
            grabber.Encoding = Encoding.UTF8;

            if (grabber.GetResponse())
                return grabber.GetXML();
            else
                return null;
        }

    }
}
